package com.tusuo.stds.ui.account;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.gender.SetGenderActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import java.io.File;

import myandroid.library.util.ApplicationHelper;

import static com.tusuo.stds.Constants.AVATAR_FILE;

@EActivity(R.layout.activity_account)
public class AccountActivity extends BaseToolbarActivity<AccountView, AccountPresenter>
                                implements AccountView {

    @ViewById(R.id.account_iv_avatar)
    ImageView ivAvatar;
    @ViewById(R.id.account_tv_name)
    TextView tvName;
    @ViewById(R.id.account_tv_gender)
    TextView tvGender;
    @ViewById(R.id.account_tv_school)
    TextView tvSchool;

    @StringRes(R.string.toolbar_title_account)
    String toolbarTitle;
    @StringRes(R.string.err_msg_server)
    String mErrMsgServer;

    @StringRes(R.string.set_gender_male)
    String mGenderMale;
    @StringRes(R.string.set_gender_female)
    String mGenderFemale;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    @AfterViews
    void initData() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, null);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public AccountPresenter createPresenter() {
        return new AccountPresenter(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);  //这一句必须的，否则Intent无法获得最新的数据
    }

    @Override
    public void onResume() {
        super.onResume();

        presenter.updateUserInfo();
    }

    @Override
    public void showAvatar(String imgUrl, boolean showPlaceholder) {
        if (showPlaceholder) {
            // 大图会产生OOM
            Glide
                    .with(this)
                    .load(Utils.getCorrectImageUrl(imgUrl))
                    .centerCrop()
                    .into(ivAvatar);
        } else {
            Glide
                    .with(this)
                    .load(Utils.getCorrectImageUrl(imgUrl))
                    .centerCrop()
                    .into(ivAvatar);
        }
    }

    @Override
    public void showAvatar(File file) {
        Glide
                .with(this)
                .load(file)
                .centerCrop()
                .into(ivAvatar);
    }

    @Override
    public void showAvatar(Integer imgResourceId) {
        Glide
                .with(this)
                .load(imgResourceId)
                .centerCrop()
                .into(ivAvatar);
    }

    @Override
    public void setName(String name) {
        if (!TextUtils.isEmpty(name)) {
            tvName.setText(Utils.Text.getTruncatedString(name, 16));
        } else {
            tvName.setText("请设置名字");
        }
    }

    @Override
    public void setGender(int gender) {
        switch (gender) {
            case SetGenderActivity_.GENDER_MALE:
                tvGender.setText(mGenderMale);
                break;
            case SetGenderActivity_.GENDER_FEMALE:
                tvGender.setText(mGenderFemale);
                break;
            default:
                tvGender.setText("未知");
                break;
        }
    }

    @Override
    public void setSchoolName(String schoolName) {
        tvSchool.setText(schoolName);
    }

    @Click(R.id.account_layout_avatar)
    void toSetAvatar() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toAvatar();
    }

    @Click(R.id.account_layout_name)
    void toSetName() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toName();
    }

    @Click(R.id.account_layout_gender)
    void toSetGender() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toGender();
    }

    @Click(R.id.account_layout_school)
    void toSetSchool() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toSchool();
    }

    @Click(R.id.account_layout_binding)
    void toSetThirdBindings() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.toBindThird();
    }
}
